﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LDM.Framework.Caches
{
    /// <summary>
    /// 提供本地内存缓存功能
    /// </summary>
    public interface IMemoryCache
    {
        /// <summary>
        /// 从缓存获取数据
        /// </summary>
        /// <typeparam name="T">缓存对象类型</typeparam>
        /// <param name="key">缓存key</param>
        /// <returns>数据结果</returns>
        T Get<T>(string key) where T : class;

        /// <summary>
        /// 更新缓存数据
        /// </summary>
        /// <typeparam name="T">缓存对象类型</typeparam>
        /// <param name="key">缓存key</param>
        /// <param name="obj">缓存数据</param>
        /// <param name="durationMinute">缓存的存储时间，单位：分钟</param>
        /// <returns>数据结果</returns>
        void Set<T>(string key, T obj, double? durationMinute = null) where T : class;

        /// <summary>
        /// 从缓存获取数据，如果缓存没有的话则从调用getFromPersistence方法获取数据并加入缓存
        /// </summary>
        /// <typeparam name="T">缓存对象类型</typeparam>
        /// <param name="key">缓存key</param>
        /// <param name="factory">缓存没有的时候调用的方法</param>
        /// <param name="durationMinute">缓存的存储时间，单位：分钟</param>
        /// <returns>数据结果</returns>
        T GetFromCacheFirst<T>(string key, Func<T> factory, double? durationMinute = null) where T : class;

        /// <summary>
        /// 根据key删除缓存
        /// </summary>
        /// <param name="key">需要删除的key</param>
        void RemoveCache(string key);
    }
}
